﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace belov_test
{
    public partial class InformationPanel : UserControl
    {
        public const int DEFAULT_HEIGHT = 110;
        private const int DEFAULT_PADDING = 15;
        private const int LEFT_PADDING = 25;
        private const int DEFAULT_LINE_DISTANCE = 20;
        private const int DEFAULT_BTN_WIDTH = 100;
        private const int DEFAULT_BTN_HEIGHT = 35;

        private const string MB_EXIT_TEST_TEXT = "Вы действительно хотите выйти?\n" + 
                                                 "Весь прогресс прохождения теста будет утерян";
        private const string MB_EXIT_PROGRAM_TEXT = "Вы действительно хотите выйти из программы?";
        private const string MB_EXIT_USER_TEXT = "Вы действительно хотите выйти из своей учетной записи?";
        private const string MB_CAPTION = "Подтверждение выхода";
        private const string MB_TIMER_ELAPSED_CAPTION = "Информация";

        private delegate void ShowRestartTestMessageDelegate();

        public event EventHandler ExitTest;
        public event EventHandler ExitUserAccount;
        public event EventHandler ExitProgram;
        public event EventHandler RestartTest;
        public event EventHandler ShowPreviousTest;

        
        private Label m_userNameLabel;
        private Label m_userName;
        private Label m_prevDateLabel;
        private Label m_prevDate;
        private Button m_exitTestBtn;
        private Button m_exitProgramBtn;
        private Button m_exitUserBtn;
        private Button m_showPreviousTest;

        public InformationPanel()
        {
            InitializeComponent();

            CreatePanel();

            Resize += new EventHandler(OnResize);
        }

        public void SetAdminLogin(String login)
        {
            m_userNameLabel.Text = "Администратор: " + login;
            m_prevDateLabel.Visible = false;
        }

        private void OnResize(object sender, EventArgs e)
        {
            
            m_userNameLabel.Location = new Point(LEFT_PADDING, DEFAULT_PADDING);
            m_userName.Location = new Point(m_userNameLabel.Location.X + m_userNameLabel.Width, m_userNameLabel.Location.Y);
            m_prevDateLabel.Location = new Point(LEFT_PADDING, m_userNameLabel.Location.Y + DEFAULT_LINE_DISTANCE);
            m_prevDate.Location = new Point(m_prevDateLabel.Location.X + m_prevDateLabel.Width, m_prevDateLabel.Location.Y);
            m_exitTestBtn.Location = new Point(Width - m_exitTestBtn.Width - DEFAULT_PADDING, Height - m_exitTestBtn.Height - DEFAULT_PADDING);
            m_exitProgramBtn.Location = m_exitTestBtn.Location;
            m_exitUserBtn.Location = new Point(m_exitTestBtn.Location.X, m_exitTestBtn.Location.Y - DEFAULT_PADDING - m_exitUserBtn.Height);
            if (m_showPreviousTest != null)
            {
                m_showPreviousTest.Location = new Point(m_exitTestBtn.Location.X - m_exitProgramBtn.Width - 10, m_exitTestBtn.Location.Y - DEFAULT_PADDING - m_exitUserBtn.Height);
            }
        }

        private void CreatePanel()
        {
            

            Font labelFont = new Font("Microsoft Sans Serif", 9, FontStyle.Bold);
            Font dataFont = new Font("Microsoft Sans Serif", 10, FontStyle.Italic);

            m_userNameLabel = CreateLabel("Имя пользователя: ", labelFont);
            m_userName = CreateLabel("", dataFont);
            m_prevDateLabel = CreateLabel("Последние действия с учетной записью: ", labelFont);
            m_prevDate = CreateLabel("", dataFont);

            m_exitTestBtn = CreateButton("Выйти в меню", OnExitTestBtnClick);
            m_exitProgramBtn = CreateButton("Выход из программы", OnExitProgramBtnClick);
            m_exitUserBtn = CreateButton("Выход из учетной записи", OnExitUserBtnClick);
            OnResize(EventArgs.Empty);
        }

        public void AddPrevTestsButton()
        {
            m_showPreviousTest = CreateButton("Статитика по тестам", OnShowPreviousTest);
            m_showPreviousTest.Location = new Point(m_exitTestBtn.Location.X - m_exitProgramBtn.Width - 10, m_exitTestBtn.Location.Y - DEFAULT_PADDING - m_exitUserBtn.Height);
        }

        public void RemovePrevTestsButton()
        {
            Controls.Remove(m_showPreviousTest);
            m_showPreviousTest = null;
        }

        private void OnTimerElapsed(object sender, EventArgs e)
        {
            if (this.InvokeRequired)
            {
                ShowRestartTestMessageDelegate showRestartMessageDelegate = new ShowRestartTestMessageDelegate(ShowRestartTestMessage);
                this.Invoke(showRestartMessageDelegate);
            }
            else
            {
                ShowRestartTestMessage();
            }
        }

        private void ShowRestartTestMessage()
        {
        }

        private Button CreateButton(string text, EventHandler clickHandler)
        {
            Button button = new Button();
            button.Text = text;
            button.Click += new EventHandler(clickHandler);
            button.Width = DEFAULT_BTN_WIDTH;
            button.Height = DEFAULT_BTN_HEIGHT;
            Controls.Add(button);

            return button;
        }

        private Label CreateLabel(string text, Font font)
        {
            Label label = new Label();
            label.Text = text;
            label.AutoSize = true;
            label.Font = font;
            Controls.Add(label);

            return label;
        }

        private void OnExitTestBtnClick(object sender, EventArgs e)
        {
            if (ShowQuestionMessageBox(MB_EXIT_TEST_TEXT, MB_CAPTION) == DialogResult.Yes)
            {
                EventHandler handler = ExitTest;
                if (handler != null)
                {
                    handler(this, EventArgs.Empty);
                }
            }
        }

        private DialogResult ShowQuestionMessageBox(string text, string caption)
        {
            return MessageBox.Show(
                            text,
                            caption,
                            MessageBoxButtons.YesNo,
                            MessageBoxIcon.Question
                            );
        }

        private void OnExitProgramBtnClick(object sender, EventArgs e)
        {
            if (ShowQuestionMessageBox(MB_EXIT_PROGRAM_TEXT, MB_CAPTION) == DialogResult.Yes)
            {
                EventHandler handler = ExitProgram;
                if (handler != null)
                {
                    handler(this, EventArgs.Empty);
                }

                Environment.Exit(0);
            }
        }

        private void OnExitUserBtnClick(object sender, EventArgs e)
        {
            if (ShowQuestionMessageBox(MB_EXIT_USER_TEXT, MB_CAPTION) == DialogResult.Yes)
            {
                EventHandler handler = ExitUserAccount;
                if (handler != null)
                {
                    handler(this, EventArgs.Empty);
                }
            }
        }

        private void OnShowPreviousTest(object sender, EventArgs e)
        {
            EventHandler handler = ShowPreviousTest;
            if (handler != null)
            {
                handler(sender, EventArgs.Empty);
            }
        }

        public void SwitchExtraComponentsVisible(bool value)
        {
            m_exitTestBtn.Visible = value;
            m_exitProgramBtn.Visible = !value;
            m_exitUserBtn.Visible = !value;
        }

        

        public void DestroyPanel()
        {
            Controls.Clear();
        }

        public void SetUserInfo(UserInfo userInfo)
        {
            m_userName.Text = userInfo.Surname + " " + userInfo.Name + " " + userInfo.Patronymic;
            m_prevDate.Text = userInfo.LastAccountUsing;
        }

        public void ResetInfo()
        {
            m_userName.Text = "";
            m_prevDate.Text = "";
        }
    }
}
